<?php

namespace App\Http\Controllers;

use App\Banner;
use Illuminate\Http\Request;

class BannerController extends Controller
{
    // 保存
    public function save(Request $request) {
        $data = $request->all();
        $banner = Banner::query()->find($request->get('id'));
        if ($banner) {
            $banner->update($data);
            return response()->json(['code' => '200', 'msg' => '更新成功']);
        }
        Banner::query()->create($data);
        return response()->json(['code' => '200', 'msg' => '插入成功']);
    }

    // 删除（根据ID）
    public function delete(Request $request) {
        $id = $request->get('id');
        Banner::destroy($id);
        return response()->json(['code' => '200', 'msg' => '删除成功']);
    }

    // 查询全部
    public function list() {
        $res = Banner::query()->get();
        return response()->json(['code' => '200', 'msg' => '获取成功', 'data' => $res]);
    }

    // 查询单个（根据ID）
    public function one(Request $request) {
        $res = Banner::query()->find($request->get('id'));
        return response()->json(['code' => '200', 'msg' => '获取成功', 'data' => $res]);
    }

}
